package com.dream.testtubebaby.fragment;

import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dream.testtubebaby.adapter.BbsAllBaseAdapter;
import com.dream.testtubebaby.adapter.BbsBaseAdapter;
import com.dream.testtubebaby.data.bbs.BbsNode;
import com.dream.testtubebaby.data.bbs.HotNode;
import com.dream.testtubebaby.utils.AppUtil;
import com.dream.testtubebaby.utils.NetUtil;

public class BbsAllFragment extends BbsSubBaseFragment {

	public BbsAllFragment() {
	}

	public BbsAllFragment(int newsType) {
		super(newsType);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected void initListView() {
		super.initListView();
		mBbsListView.setDividerHeight(0);
	}

	@Override
	public void onPause() {
		super.onPause();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public void onStart() {
		super.onStart();
//		currentPage = 1;
		// mBbsListView.setOnItemClickListener(new OnItemClickListener() {
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view,
		// int position, long id) {
		// BbsNode mBbsListItem = (BbsNode)mAdapter.getItem(position - 1);
		// Intent intent = new Intent(getActivity(),
		// BbsListItemDetailActivity.class);
		// intent.putExtra(BbsListItemDetailActivity._ID, mBbsListItem.getId());
		// startActivity(intent);
		// }
		//
		// });
	}

	@Override
	protected BbsBaseAdapter getBaseAdapter() {
		return new BbsAllBaseAdapter(getActivity(), getBbsListItems());
	}

	@Override
	protected void loadMoreData() {
		if (isLoadingDataFromNetWork) {
			currentPage += 1;
			try {
				if (mAdapter.getCount() != 0) {
					BbsNode bbsNode = (BbsNode) mAdapter.getItem(mAdapter
							.getCount() - 1);
					if (bbsNode.getId() != 1) {
						List<BbsNode> newsItems = downLoadBbsListItem(bbsNode
								.getId());
						bbsItemDao.add(newsItems);
						mAdapter.addAll(newsItems);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			currentPage += 1;
			List<BbsNode> newsItems = bbsItemDao.list(newsType, currentPage);
			mAdapter.addAll(newsItems);
		}
	}

	@Override
	protected int refreashData() {
		if (NetUtil.checkNet(getActivity())) {
			try {
				List<BbsNode> newsItems = downLoadBbsListItem(-1);
				if (newsItems.size() != 0) {
					List<HotNode> hotNodes = downLoadBbsTopListItem();
					newsItems.get(0).addHots(hotNodes);
				}
				mAdapter.setDatas(newsItems);

				currentPage = 1;
				isLoadingDataFromNetWork = true;
				AppUtil.setRefreashTime(getActivity(), newsType);
				bbsItemDao.deleteAll(newsType);
				bbsItemDao.add(newsItems);
			} catch (Exception e) {
				e.printStackTrace();
				isLoadingDataFromNetWork = false;
				return TIP_ERROR_SERVER;
			}
		} else {
			currentPage = 1;
			isLoadingDataFromNetWork = false;
			List<BbsNode> newsItems = bbsItemDao.list(newsType, currentPage);
			mDatas = newsItems;
//			mAdapter.setDatas(newsItems);
			return TIP_ERROR_NO_NETWORK;
		}

		return -1;
	}
}
